package com.OrderingSystem.controller.buyer;

import com.OrderingSystem.annotation.LoggerManage;
import com.OrderingSystem.converter.OrderForm2OrderDTOConverter;
import com.OrderingSystem.dto.OrderDTO;
import com.OrderingSystem.dto.OrderDetailsDTO;
import com.OrderingSystem.enums.OrderStatusEnum;
import com.OrderingSystem.enums.ResultEnum;
import com.OrderingSystem.exception.SellException;
import com.OrderingSystem.pojo.*;
import com.OrderingSystem.service.IOrderMasterService;
import com.OrderingSystem.service.IProductInfoService;
import com.OrderingSystem.service.PushMessageService;
import com.OrderingSystem.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "买家订单接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200, message = "操作成功", response = ProductCategory.class),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 403, message = "权限被禁止"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
@RequestMapping("/sell/buyer")
@Slf4j
public class BuyerOrderController {

    @Autowired
    IOrderMasterService orderMasterService;

    @Autowired
    PushMessageService pushMessageService;

    @Autowired
    WebSocket webSocket;

    @LoggerManage(value = "买家下单", methodReturnType = "ResultVO")
    @ApiOperation(value = "买家下单", notes = "买家下单")
    @PostMapping("/order/create")
    public ResultVO createOrder(OrderForm orderForm){

        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        // 调用下单业务
        OrderDTO order = orderMasterService.createOrder(orderDTO);

        //发送模板消息
        pushMessageService.orderStatus(order.getOrderId());
        log.info("买家下单的数据,进行消息推送："+orderDTO);

        //给卖家推动新的订单消息
        webSocket.sendMessage(String.valueOf(order.getOrderId()));
        return new ResultVO(0, "下单成功", order);
    }

    /**
     *  http://localhost:8080/sell/buyer/product/list
     *  买家订单列表
     */
    @ApiOperation(value = "买家订单列表", notes = "买家订单列表")
    @LoggerManage(value = "买家订单列表", methodReturnType = "ResultVO")
    @GetMapping("/order/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid) {

        if (!StringUtils.hasText(openid)) {
            log.error("【查询订单列表】openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        // 创建分页对象
        List<OrderDTO> orderDTOList = orderMasterService.findList(openid);
        return new ResultVO(0,"成功",orderDTOList);
    }


    /**
     * 买家 订单详情
     * http://127.0.0.1:8080/sell/buyer/order/detail?orderId=93311934e8094c8ebac8e88df9678004&openid=1234565432
     */
    @ApiOperation(value = "买家订单详情", notes = "买家订单详情")
    @LoggerManage(value = "买家订单详情", methodReturnType = "ResultVO")
    @GetMapping("/order/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                     @RequestParam("orderId") String orderId) {

        OrderDetailsDTO orderDTO = orderMasterService.findOrderInfo(openid, orderId);
        return new ResultVO(0,"成功",orderDTO);
    }

    /**
     * 买家 取消订单
     */
    @ApiOperation(value = "买家取消订单", notes = "买家取消订单")
    @LoggerManage(value = "买家取消订单", methodReturnType = "ResultVO")
    @PostMapping("/order/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId) {

        //log.info("要取消的订单ID是:"+orderId);
        log.info("要取消的订单openid是:"+openid);
        orderMasterService.cancelOrder(openid, orderId);
        //推送消息
        pushMessageService.orderStatus(Integer.valueOf(orderId));
        return new ResultVO(0,"取消成功",null);
    }

}
